package com.example.boos.stores;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.boos.HttpUtil.MyPost;
import com.example.boos.MainActivity;
import com.example.boos.R;
import com.example.boos.context.BossInfo;
import com.example.boos.createStore.createStoreActivity;
import com.example.boos.pojo.Store;
import com.example.boos.stores.myInfo.MyInfoFragment;
import com.example.boos.stores.myInfo.MyInfoFragmentCallBack;
import com.example.boos.util.MyThreadPool;
import com.example.boos.util.RequestPermissionUtil;

import java.util.List;

public class stores_activity extends AppCompatActivity {

    public static Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    if((Boolean)msg.obj){
                        // 店铺删除成功
                        storeAdapter.update();
                        Toast.makeText(activity,"店铺删除成功！",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(activity,"店铺删除成功！",Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2:
                    storeAdapter.updateAll(BossInfo.storeList);
                    break;
                case 3:
                    Intent intent = new Intent();
                    intent.setComponent(new ComponentName(activity,MainActivity.class));
                    activity.startActivity(intent);
                    break;

            }
        }
    };

    public static Activity activity;

    private ImageView back;

    public static storeItemAdapter storeAdapter;

    private ListView storeListView;

    private MyInfoFragment fragment;

    private MyInfoFragmentCallBack myInfoFragmentCallBack = new MyInfoFragmentCallBack() {
        @Override
        public void gotoMyStore() {
            FragmentManager supportFragmentManager = getSupportFragmentManager();
            supportFragmentManager.beginTransaction().remove(fragment).commit();
            // 显示 storeListView
            storeListView.setVisibility(View.VISIBLE);
        }
    };

    //private ImageView createStore;

    public stores_activity(){
        activity = this;
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 可以适当地更新一下数据
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 让 fragment更新一下数据
        if(BossInfo.update == 1){
            BossInfo.update = 0;
            if(fragment != null){
                fragment.updateView();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_stores_activity);
        Toolbar toolbar =(Toolbar)findViewById(R.id.addressToolBar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        RequestPermissionUtil.requestPermission(this);
        // 初始化数据
        initData();
        initView();
        initListener();
    }

    //菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.store_menu,menu);
        return true;
    }

    // 监听菜单
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.createStore:
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(stores_activity.this, createStoreActivity.class));
                stores_activity.this.startActivity(intent);
                break;
            case R.id.updateMyInfo:
                // 调用FragmentManager 来显示Fragment
                fragment = new MyInfoFragment(this,myInfoFragmentCallBack);
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.show_myInfoFragment,fragment).commitNow();
                storeListView.setVisibility(View.GONE);
                // 为 back 切换一下监听器
                back.setOnClickListener(backToStoreList);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData(){
        // 获取到商家的所有店铺
        MyThreadPool.doJob(new Runnable() {
            @Override
            public void run() {
                BossInfo.storeList = (List<Store>) MyPost.doPost("getAllStore", BossInfo.bossId);
                Message msg = new Message();
                msg.what = 2;
                handler.sendMessage(msg);
                // 通知刷新
            }
        });
    }

    private void initView(){
        // 初始化组件
        storeListView = findViewById(R.id.myStores);
        storeAdapter = new storeItemAdapter(BossInfo.storeList,this);
        storeListView.setAdapter(storeAdapter);
        back = findViewById(R.id.myAddressBack);
        //createStore = findViewById(R.id.createStore);

    }

    private void initListener(){
        back.setOnClickListener(backToLogin);
    }

    // 返回到登录页面
    View.OnClickListener backToLogin = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
    };

    // 从个人信息页面返回到 店铺列表页面
    View.OnClickListener backToStoreList = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // 怎么返回 移除 Fragment就行了
            getSupportFragmentManager().beginTransaction().remove(fragment).commitNow();
            // 显示一下店铺列表
            storeListView.setVisibility(View.VISIBLE);
            back.setOnClickListener(backToLogin);
        }
    };


}